{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# F-strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Guido is an advanced Python programmer!\n"
     ]
    }
   ],
   "source": [
    "name = \"Guido\"\n",
    "level = \"advanced\"\n",
    "print(f\"{name} is an {level} Python programmer!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Combine expressions directly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age = 30\n",
    "print(f\"In 10 years, I will be {age + 10} years old.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use f-strings to format numbers with precision or in human-readable formats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pi rounded to two decimals: 3.14\n"
     ]
    }
   ],
   "source": [
    "pi = 3.14159\n",
    "print(f\"Pi rounded to two decimals: {pi:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add thousands separators:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "large_number = 123456789\n",
    "print(f\"Large number with commas: {large_number:,}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Display percentages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "success_rate = 0.874\n",
    "print(f\"Success rate: {success_rate:.1%}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use f-strings with `datetime` objects for cleaner formatting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current date: 2024-12-17\n",
      "Current time: 11:13:32\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "now = datetime.now()\n",
    "print(f\"Current date: {now:%Y-%m-%d}\")\n",
    "print(f\"Current time: {now:%H:%M:%S}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Align text for neatly formatted outputs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python     is left-aligned\n",
      "    Python is right-aligned\n",
      "  Python   is centered\n"
     ]
    }
   ],
   "source": [
    "name = \"Python\"\n",
    "print(f\"{name:<10} is left-aligned\")  # Output: Python     is left-aligned\n",
    "print(f\"{name:>10} is right-aligned\")  # Output:      Python is right-aligned\n",
    "print(f\"{name:^10} is centered\")  # Output:   Python   is centered"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Dynamic width formatting:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            Arjan             \n"
     ]
    }
   ],
   "source": [
    "name = \"Arjan\"\n",
    "width = 30\n",
    "print(f\"{name:^{width}}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "F-strings support a debugging syntax that displays both the expression and its result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "value=42\n"
     ]
    }
   ],
   "source": [
    "value = 42\n",
    "print(f\"{value=}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
